// pages/sale/afterRefund/afterRefund.js
var app = getApp()
const $api = require('../../../utils/api.js').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件所需的参数
    navbarData: {
      havebg: 1, //1表示不显示    0表示显示
      out: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '申请退款', //导航栏 中间的标题
      bg: '#fff', //导航背景
    },
    height: app.globalData.height,
    goodsStateList: [{
      type: 1,
      name: '未收到货',
      icon: false
    }, {
      type: 2,
      name: '已收到货',
      icon: false
    }],
    goodsState: '', //货物状态id
    CSgoodsState: '',
    goodsStateText: '请选择',
    CSgoodsStateText: '',
    refundState: '', //退款原因id
    CSrefundState: '',
    refundStateText: '请选择',
    CSrefundStateText: '',
    desc: '', //描述
    images: [], //图片

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      asale_type: options.asale_type,
      goods: wx.getStorageSync('goods'),
      price: wx.getStorageSync('price'),
    })
  },
  //货物状态弹出
  onGoodsStateModal(e) {
    var _this = this;
    for (var i = 0; i < _this.data.goodsStateList.length; i++) {
      if (_this.data.goodsState == _this.data.goodsStateList[i].type) {
        _this.setData({
          ['goodsStateList[' + i + '].icon']: true
        })
      }
    }
    _this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  //选择货物状态
  onGoodsState(e) {
    var _this = this;
    for (var i = 0; i < _this.data.goodsStateList.length; i++) {
      if (_this.data.goodsStateList[i].type == e.currentTarget.dataset.type) {
        _this.setData({
          ['goodsStateList[' + i + '].icon']: true
        })
      } else {
        _this.setData({
          ['goodsStateList[' + i + '].icon']: false
        })
      }
    }
    _this.setData({
      CSgoodsState: e.currentTarget.dataset.type,
      CSgoodsStateText: e.currentTarget.dataset.text,
    })
  },
  //货物状态完成
  onGoodsStateComplete() {
    var _this = this;
    if (_this.data.CSgoodsState == '') {
      wx.showToast({
        title: '请选择货物状态',
        icon: 'none',
        duration: 2000
      })
    } else {
      _this.setData({
        loadModal: true
      })
      $api.getAsaleReason({
        login_token: wx.getStorageSync('login_token'),
        type: _this.data.CSgoodsState,
      }).then((res) => {
        for (var i = 0; i < res.data.data.length; i++) {
          res.data.data[i].icon = false
        }
        _this.setData({
          goodsState: _this.data.CSgoodsState,
          goodsStateText: _this.data.CSgoodsStateText,
          modalName: null,
          refundList: res.data.data,
          CSrefundState: '',
          CSrefundStateText: '',
          refundState: '',
          refundStateText: '请选择',
          loadModal: false
        })
      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })
    }

  },
  //关闭货物状态弹框
  onGoodsStateClose() {
    var _this = this
    for (var i = 0; i < _this.data.goodsStateList.length; i++) {
      if (_this.data.CSgoodsState == _this.data.goodsStateList[i].type) {
        _this.setData({
          ['goodsStateList[' + i + '].icon']: false
        })
      }
    }
    _this.setData({
      modalName: null,
    })
  },
  //退款原因弹出
  onRefundModal(e) {
    var _this = this;
    if (_this.data.goodsState == '') {
      wx.showToast({
        title: '请先选择货物状态',
        icon: 'none',
        duration: 2000
      })
    } else {
      for (var i = 0; i < _this.data.refundList.length; i++) {
        if (_this.data.refundState == _this.data.refundList[i].id) {
          _this.setData({
            ['refundList[' + i + '].icon']: true
          })
        }
      }
      _this.setData({
        modalName: e.currentTarget.dataset.target
      })
    }

  },
  //选择退款原因
  onRefundState(e) {
    var _this = this;
    console.log(e)
    for (var i = 0; i < _this.data.refundList.length; i++) {
      if (_this.data.refundList[i].id == e.currentTarget.dataset.id) {
        _this.setData({
          ['refundList[' + i + '].icon']: true
        })
      } else {
        _this.setData({
          ['refundList[' + i + '].icon']: false
        })
      }
    }
    _this.setData({
      CSrefundState: e.currentTarget.dataset.id,
      CSrefundStateText: e.currentTarget.dataset.text,
    })

  },
  //退款原因完成
  onRefundStateComplete() {
    var _this = this;
    if (_this.data.CSrefundState == '') {
      wx.showToast({
        title: '请选择退款原因',
        icon: 'none',
        duration: 2000
      })
    } else {
      _this.setData({
        refundState: _this.data.CSrefundState,
        refundStateText: _this.data.CSrefundStateText,
        modalName: null,
      })
    }
  },
  //关闭退款原因弹框
  onRefundStateClos(e) {
    var _this = this;
    for (var i = 0; i < _this.data.refundList.length; i++) {
      if (_this.data.CSrefundState == _this.data.refundList[i].id) {
        _this.setData({
          ['refundList[' + i + '].icon']: false
        })
      }
    }
    _this.setData({
      modalName: null,
    })
  },
  //获取描述
  onDesc(e) {
    this.setData({
      desc: e.detail.value
    })
  },
  //上传图片
  onImages() {
    var _this = this;
    _this.setData({
      loadModal: true
    })
    $api.uploadImages().then((res) => {
      _this.setData({
        images: _this.data.images.concat(res.url),
        loadModal: false
      })
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })

  },
  //删除图片
  onImagesClose(e) {
    var _this = this;
    var images = _this.data.images;
    images.splice(e.currentTarget.dataset.idx, 1);
    _this.setData({
      images: images
    })
  },
  //提交
  onSubmit() {
    var _this = this;
    if (_this.data.goodsState == '') {
      wx.showToast({
        title: '请选择货物状态',
        icon: 'none',
        duration: 2000
      })
    } else if(_this.data.refundState==''){
      wx.showToast({
        title: '请选择退款原因',
        icon: 'none',
        duration: 2000
      })
    }else {
      _this.setData({
        loadModal:true
      })
      var details = [];
      for (var i = 0; i < _this.data.goods.length; i++) {
        details = details.concat(_this.data.goods[i].id)
      }
      details=JSON.stringify(details)
      var images=JSON.stringify(_this.data.images);
      if(_this.data.goodsState==1){
        var goods_status=0
      }else{
        var goods_status=1
      }
      $api.submitAsale({
        login_token: wx.getStorageSync('login_token'),
        order_id: _this.data.goods[0].order_id,
        asale_type: _this.data.asale_type,
        desc: _this.data.desc,
        images: images,
        reason_id: _this.data.refundState,
        goods_status: goods_status,
        refund_money: _this.data.price,
        details: details
      }).then((res) => {
        _this.setData({
          loadModal: false
        })
        wx.showToast({
          title: '提交成功',
          icon: 'none',
          duration: 2000
        })
        
        setTimeout(function(){
          wx.redirectTo({
            url: '/pages/sale/refundDetail/refundDetail?id='+res.data.data.asale_id,
          })
        },1500)
      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })


    }
  },
  //查看图片
  onSeeImage(e){
    wx.previewImage({
      current: e.currentTarget.dataset.url, 
      urls: [e.currentTarget.dataset.url] 
    })
  },
  scroll: function (e) {
    // console.log(e)
    this.setData({
      scrollTop: e.detail.scrollTop
    });
    if (e.detail.scrollTop >= 40) {
      this.setData({
        headerBg: true
      })
    } else {
      this.setData({
        headerBg: false
      })
    }
  },
  //返回上一页
  out() {
    wx.navigateBack({
      delta: 1,
    })
  },


})